# Same as aggregate_inequality, but for groups in figure 8.
set.seed(102112516)

# Set size of pseudo sample
M <- 10000

years <- seq(from = 1989,to = 2022,by = 3)
imps_race <- vector(mode="list", length = 12)
names(imps_race) <- as.character(years)
imps_age <- vector(mode="list", length = 12)
names(imps_age) <- as.character(years)

for (k in 1:12){
  print(paste("Year: ",as.character(years[k]),sep = ""))
  inc_wealth_w <- vector(mode = "list",length = 5)
  inc_wealth_b <- vector(mode = "list",length = 5)
  inc_wealth_y <- vector(mode = "list",length = 5)
  inc_wealth_o <- vector(mode = "list",length = 5)
  
if (k<7){
  for (i in 1:5){
    print(paste("Impute: ",as.character(i),sep = ""))
    # White
      Q <- data.frame(scf89_22[[k]][[i]])
      colnames(Q)[3] <- "RACE"
      Q <- dplyr::filter(Q,RACE==1)
      W <- Q[,1]/sum(Q[,1])
      ot <- vquantile(Q[,c(4,5)],W)
      inc_wealth_w[[i]] <- ILF(Q[,c(4,5)],W,ot,M)
    # Black
      Q <- data.frame(scf89_22[[k]][[i]])
      colnames(Q)[3] <- "RACE"
      Q <- dplyr::filter(Q,RACE==2)
      W <- Q[,1]/sum(Q[,1])
      ot <- vquantile(Q[,c(4,5)],W)
      inc_wealth_b[[i]] <- ILF(Q[,c(4,5)],W,ot,M)
    # Working Age
      Q <- data.frame(scf89_22[[k]][[i]])
      colnames(Q)[2] <- "AGE"
      Q <- dplyr::filter(Q,AGE < 65)
      W <- Q[,1]/sum(Q[,1])
      ot <- vquantile(Q[,c(4,5)],W)
      inc_wealth_y[[i]] <- ILF(Q[,c(4,5)],W,ot,M)
    # Retiring Age      
      Q <- data.frame(scf89_22[[k]][[i]])
      colnames(Q)[2] <- "AGE"
      Q <- dplyr::filter(Q,AGE > 64)
      W <- Q[,1]/sum(Q[,1])
      ot <- vquantile(Q[,c(4,5)],W)
      inc_wealth_o[[i]] <- ILF(Q[,c(4,5)],W,ot,M)
   }
  }
  if (k > 6){ # These years require a small amount of noise for stability
    for (i in 1:5){
      print(paste("Impute: ",as.character(i),sep = ""))
      # White
        Q <- data.frame(scf89_22[[k]][[i]])
        colnames(Q)[3] <- "RACE"
        Q <- dplyr::filter(Q,RACE==1)
        W <- Q[,1]/sum(Q[,1])
        X <- Q[,4:5]
        mu <- cbind(X[,1]%*%W, X[,2]%*%W)
        X <- cbind(X[,1]/mu[1],X[,2]/mu[2])
        X <- X + cbind(runif(nrow(X), 0,0.01),runif(nrow(X), 0,0.05))
        ot <- vquantile(X,W)
        inc_wealth_w[[i]] <- ILF(X,W,ot,M)
      # Black    
        Q <- data.frame(scf89_22[[k]][[i]])
        colnames(Q)[3] <- "RACE"
        Q <- dplyr::filter(Q,RACE==2)
        W <- Q[,1]/sum(Q[,1])
        X <- Q[,4:5]
        mu <- cbind(X[,1]%*%W, X[,2]%*%W)
        X <- cbind(X[,1]/mu[1],X[,2]/mu[2])
        X <- X + cbind(runif(nrow(X), 0,0.01),runif(nrow(X), 0,0.05))
        ot <- vquantile(X,W)
        inc_wealth_b[[i]] <- ILF(X,W,ot,M)
      # Working Age
        Q <- data.frame(scf89_22[[k]][[i]])
        colnames(Q)[2] <- "AGE"
        Q <- dplyr::filter(Q,AGE < 65)
        W <- Q[,1]/sum(Q[,1])
        X <- Q[,4:5]      
        mu <- cbind(X[,1]%*%W, X[,2]%*%W)
        X <- cbind(X[,1]/mu[1],X[,2]/mu[2])
        X <- X + cbind(runif(nrow(X), 0,0.01),runif(nrow(X), 0,0.05))
        ot <- vquantile(X,W)
        inc_wealth_y[[i]] <- ILF(X,W,ot,M)
      # Retiring Age       
        Q <- data.frame(scf89_22[[k]][[i]])
        colnames(Q)[2] <- "AGE"
        Q <- dplyr::filter(Q,AGE > 64)
        W <- Q[,1]/sum(Q[,1])
        X <- Q[,4:5]      
        mu <- cbind(X[,1]%*%W, X[,2]%*%W)
        X <- cbind(X[,1]/mu[1],X[,2]/mu[2])
        X <- X + cbind(runif(nrow(X), 0,0.01),runif(nrow(X), 0,0.05))
        ot <- vquantile(X,W)
        inc_wealth_o[[i]] <- ILF(X,W,ot,M)
    }
    
    
    
  }
    
  imps_race[[k]] <- list(white = inc_wealth_w, black = inc_wealth_b)
  imps_age[[k]] <- list(working = inc_wealth_y, retired = inc_wealth_o)
}
